import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import java.util.regex.Pattern;

// 登录注册系统的主界面
public class LoginRegisterGUI extends JFrame {
    private JTextField usernameField;
    private JPasswordField passwordField;
    private UserManager userManager;
    private BufferedImage backgroundImage;
    private static final Pattern PASSWORD_PATTERN = Pattern.compile("^.{5,}$");
// 构造函数，初始化用户管理器和背景图片
    public LoginRegisterGUI() {
        userManager = new UserManager();
        try {
            backgroundImage = ImageIO.read(new File("images/background.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        initialize();
    }

    private void initialize() {
        setTitle("注册登录系统");
        setSize(800, 600);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());

        JLabel backgroundLabel = new JLabel() {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                if (backgroundImage != null) {
                    Graphics2D g2d = (Graphics2D) g;
                    int width = getWidth();
                    int height = getHeight();
                    g2d.drawImage(backgroundImage, 0, 0, width, height, this);
                }
            }
        };

        backgroundLabel.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(5, 5, 5, 5);

        // 设置字体，黑色加粗 20
        Font customFont = new Font("微软雅黑", Font.BOLD, 24);
        Color customColor = Color.BLACK;

        // 用户名输入框
        gbc.gridx = 0;
        gbc.gridy = 0;
        JLabel usernameLabel = new JLabel("用户名:");
        usernameLabel.setFont(customFont);
        usernameLabel.setForeground(customColor);
        backgroundLabel.add(usernameLabel, gbc);
        usernameField = new JTextField(20);
        usernameField.setFont(customFont);
        usernameField.setForeground(customColor);
        gbc.gridx = 1;
        backgroundLabel.add(usernameField, gbc);

        // 密码输入框
        gbc.gridx = 0;
        gbc.gridy = 1;
        JLabel passwordLabel = new JLabel("密码:");
        passwordLabel.setFont(customFont);
        passwordLabel.setForeground(customColor);
        backgroundLabel.add(passwordLabel, gbc);
        passwordField = new JPasswordField(20);
        passwordField.setFont(customFont);
        passwordField.setForeground(customColor);
        gbc.gridx = 1;
        backgroundLabel.add(passwordField, gbc);

        // 注册按钮
        gbc.gridx = 0;
        gbc.gridy = 2;
        // 增加左边距，使按钮右移
        gbc.insets = new Insets(5, 100, 5, 5);
        JButton registerButton = new JButton("注册");
        registerButton.setFont(customFont);
        registerButton.setForeground(customColor);
        registerButton.addActionListener(new RegisterButtonListener());
        backgroundLabel.add(registerButton, gbc);

        // 登录按钮
        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.insets = new Insets(5, 5, 5, 5);
        JButton loginButton = new JButton("登录");
        loginButton.setFont(customFont);
        loginButton.setForeground(customColor);
        loginButton.addActionListener(new LoginButtonListener());
        backgroundLabel.add(loginButton, gbc);

        add(backgroundLabel, BorderLayout.CENTER);

        // 监听窗口大小改变事件，重绘背景图
        addComponentListener(new ComponentAdapter() {
            @Override
            public void componentResized(ComponentEvent e) {
                backgroundLabel.repaint();
            }
        });

        // 使窗口居中
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int x = (screenSize.width - getWidth()) / 2;
        int y = (screenSize.height - getHeight()) / 2;
        setLocation(x, y);

        setVisible(true);
    }

    private class RegisterButtonListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            String username = usernameField.getText();
            String password = new String(passwordField.getPassword());
            if (!PASSWORD_PATTERN.matcher(password).matches()) {
                JOptionPane.showMessageDialog(LoginRegisterGUI.this, "密码长度至少为五位，请重新输入。", "错误", JOptionPane.ERROR_MESSAGE);
                return;
            }
            if (userManager.register(username, password)) {
                JOptionPane.showMessageDialog(LoginRegisterGUI.this, "注册成功！", "提示", JOptionPane.INFORMATION_MESSAGE);
            } else {
                JOptionPane.showMessageDialog(LoginRegisterGUI.this, "用户名已存在或密码长度不足，请选择其他用户名或修改密码。", "错误", JOptionPane.ERROR_MESSAGE);
            }
        }
    }

    private class LoginButtonListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            String username = usernameField.getText();
            String password = new String(passwordField.getPassword());
            if (userManager.login(username, password)) {
                JOptionPane.showMessageDialog(LoginRegisterGUI.this, "登录成功！", "提示", JOptionPane.INFORMATION_MESSAGE);
                dispose(); // 关闭登录注册界面
                new LotteryGUI(); // 打开抽奖界面
            } else {
                JOptionPane.showMessageDialog(LoginRegisterGUI.this, "用户名或密码错误，请重试。", "错误", JOptionPane.ERROR_MESSAGE);
            }
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new LoginRegisterGUI());
    }
}